package com.mlink.ai.chat.network

import android.util.Log
import com.mlink.ai.chat.assistant.robot.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitUtil {

    val retrofit: Retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(BuildConfig.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(getOkhttpClient())
            .build()
    }


    private fun getOkhttpClient(): OkHttpClient {
        val builder = OkHttpClient.Builder()
        builder.addInterceptor(CommonHeaderInterceptor())
        builder.connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)

        val logger =
            HttpLoggingInterceptor.Logger { message ->
                Log.i(
                    "RetrofitLog",
                    "retrofitBack: $message"
                )
            }
        val loggingInterceptor = HttpLoggingInterceptor(logger)
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        if (BuildConfig.IS_DEV) {
            builder.addInterceptor(loggingInterceptor)
        }

        return builder.build()
    }

}